package com.encryption.feature.doc
{
	import com.encryption.controls.FlexPaperViewerPage;
	import com.encryption.framework.constant.FileActionConstant;
	import com.encryption.rpc.cache.FileLevelManager;
	import com.encryption.rpc.facade.FileLogFacade;
	import com.encryption.rpc.model.File;
	import com.encryption.rpc.model.FileLog;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;

	public class DocViewPopUpManager
	{
		private var docViewPage:FlexPaperViewerPage;
		
		public function DocViewPopUpManager()
		{
		}
		
		public function centerPopUp(file:File):FlexPaperViewerPage
		{
			var url:String = file.swfUrl;
			docViewPage = new FlexPaperViewerPage();
			docViewPage.width = FlexGlobals.topLevelApplication.stage.width * 0.7;
			docViewPage.height = FlexGlobals.topLevelApplication.stage.height;
			
			FlexGlobals.topLevelApplication.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandle);
//				FlexGlobals.topLevelApplication.stage.addEventListener(ResizeEvent.RESIZE, resizeHandle);
			
			docViewPage._SwfFile = getUrlPath() + url;
			docViewPage.print_visible = file.canPrint;
			
			PopUpManager.addPopUp(docViewPage, FlexGlobals.topLevelApplication as UIComponent,true);
			PopUpManager.centerPopUp(docViewPage);
			
			FileLogManager.instance.addLog(file.id, FileActionConstant.ACTION_VIEW);
			return docViewPage;
		}
		
		private function fullScreenHandle(event:FullScreenEvent):void
		{
			var stage:Stage = FlexGlobals.topLevelApplication.stage;
			if(event.fullScreen && docViewPage.isPopUp){
				docViewPage.width = stage.width;
				docViewPage.height = stage.height;
			}else if(docViewPage.isPopUp){
				docViewPage.width = stage.width * 0.7;
				docViewPage.height = stage.height;
			}
			PopUpManager.centerPopUp(docViewPage);
		}
		
		private function getUrlPath():String
		{
			var url:String = FlexGlobals.topLevelApplication.url;
			
			return url.substring(0, url.lastIndexOf("/"));
		}
	}
}